<?php
 
class Faq extends MY_Controller 
{
	var $curl = 'faq';
	var $name = 'faq';
	var $search = '';
	var $language;
	var $submenu = "_menu";
	var $id_category;
	var $category_alias;

	function __construct()
	{
		parent::__construct();
        
        $this->load->model('/public/faq_model');
        
		$this->validation->set_fields($this->faq_model->fields);

		$this->data['Title'] = T_('FAQ Manager');

		$this->language = parent::getLang();
		
		$this->name = T_('FAQ Manager');		
    }
	
	function index()
	{		
		//$config = array('per_page' => 10, 'direction' => 'ASC', 'order' => '');
		$this->faq_model->searchAll();
		//$this->search = $this->data['search'];

		if ($this->input->get('excel') == true)
		{
			$this->load->library('excel');  
			$this->load->view('/faq/excel', array('list' => $this->data['list']));	
		}
		
		$this->renderLayout();
    }

	function create()
	{
        if (count($_POST))
		{
			$rules = array('faq_question', 'faq_answer', 'faq_status');
			$this->validation->set_rules($this->faq_model->getRules($rules));

			if ($this->validation->run() === TRUE)
			{							
				$item['faq_question'] 	= $this->validation->faq_question;
				$item['faq_answer'] 	= $this->validation->faq_answer;
				$item['faq_status'] 	= $this->validation->faq_status;			
				
				if ($this->faq_model->insert($item))
				{
					redirect($this->curl . '/index/');	
				}				
			}
		}

		$this->data['Title'] .= ' / '. T_('New Item');
		$this->data['wysiwyg'] = true;
		
		$this->renderLayout();        
    }
    
	function edit($id)
	{	
		if (!empty($id))
		{
			$object = $this->faq_model->get($id);
			
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
				return false; 
			}
		
			if (!count($_POST))
			{
				$this->validation->faq_id 		= $object['faq_id']; 
				$this->validation->faq_question = $object['faq_question'];
				$this->validation->faq_answer 	= $object['faq_answer'];	
	        	$this->validation->faq_status 	= $object['faq_status'];	
			}
			else
			{
				$rules = array('faq_question', 'faq_answer', 'faq_status');
				$this->validation->set_rules($this->faq_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['faq_id'] 		= $this->validation->faq_id;
					$item['faq_question'] 	= $this->validation->faq_question;
					$item['faq_answer'] 	= $this->validation->faq_answer;	
					$item['faq_status'] 	= $this->validation->faq_status;												

					$this->faq_model->update($item);					
					
					redirect($this->curl . '/index/');				
				}
			}			
		}
           
		$this->data['Title'] .= ' / ' . $object['faq_question'] . ' / '. T_('Edit');
		$this->data['wysiwyg'] = true; 
		
		$this->renderLayout();
    }
        
	function delete($id)
	{
		$data = $this->faq_model->delete($id);

		if ($data)
		{
			redirect($this->curl . '/index');	
		}
		else
		{
			redirect($this->curl . '/edit/'. $id);
		}
	}
	        
	function deleteItems()
	{
		$data = $this->faq_model->delete($_REQUEST['items']);

		echo $data;
	}
	    
	function activity()
	{
		$data = $this->utils->publishItems($this->article_model->table, $this->article_model->prKey, $this->article_model->publishedField, $_REQUEST['items'], $_REQUEST['action']);
		echo $this->utils->php2js($data);
	}
	
	/*
	 * Static Pages 
	*/
	function view($id)
	{
		$this->data['Title'] = '';
		
		$this->data['notification'] = $this->faq_model->get($id);
		
		if (!empty($this->data['notification']))
		{
			$this->data['Title'] = $this->data['notification']->title;
		}
		
		$this->view = '/faq/view';
		
		$this->renderLayout();	
	}
}

?>